package com.lipl.base.javaee.netty.nio;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * @Auther: 15541
 * @Date: 2020/10/31 19:36
 * @Description:
 */
public class NIOFileChannel03 {

    public static void main(String[] args) throws IOException {

        FileInputStream fileInputStream = new FileInputStream("1.txt");
        FileChannel fileChannel = fileInputStream.getChannel();

        FileOutputStream fileOutputStream = new FileOutputStream("2.txt");
        FileChannel fileChannel1 = fileOutputStream.getChannel();

        ByteBuffer byteBuffer = ByteBuffer.allocate(512);

        while (true){

            byteBuffer.clear();
            int read = fileChannel.read(byteBuffer);
            System.out.println("read="+read);
            if (read == -1){
                break;
            }
            //将buffer 中的数据写入到 fileChannel1 --> 2.txt
            byteBuffer.flip();
            fileChannel1.write(byteBuffer);
        }

        fileInputStream.close();
        fileOutputStream.close();
    }
}

